package com.shenghua.controller;

import com.alibaba.fastjson.JSON;
import com.shenghua.dao.bean.UserBean;
import com.shenghua.dao.mapper.OperateLogMapper;
import com.shenghua.dao.model.OperateLog;
import com.shenghua.dao.model.User;
import com.shenghua.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/12/18.
 */
@Controller
@RequestMapping(value = "/")
//@EnableAutoConfiguration
public class IndexController {

    private Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private OperateLogMapper operateLogMapper;

    /*@RequestMapping(value = "/{pageName}.html", method = RequestMethod.POST)
    public String addMouldPage(@PathVariable("pageName")String pageName){
        return pageName;
    }*/

    @RequestMapping(value = "index/mobile")
    public String indexMoblie() {
        return "phone/index";
    }

    @RequestMapping(value = "index/normal")
    public String indexPc() {
        return "pc/index";
    }

    @RequestMapping(value = "index")
    public String indexPage() {
        return "page/index";
    }

    @RequestMapping(value = "main/mobile")
    public String mainMoblie() {
        return "phone/main";
    }

    @RequestMapping(value = "main/normal")
    public String mainPc(HttpServletRequest request, Model model) {
        //HttpSession userSession = request.getSession();
        //User user = (User) userSession.getAttribute("userSession");
        UserBean userBean = (UserBean) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        User user1 = userBean.getUser();
        if(null != user1) {
            String roleId = "";
            if(StringUtils.isNotBlank(user1.getRoleId())) {
                roleId = user1.getRoleId().toString();
            }
            model.addAttribute("roleId", roleId);
            request.setAttribute("nickname", user1.getNickname());
            logger.info("名稱：" + user1.getNickname());
        }
        return "pc/main";
    }

    @RequestMapping(value = "main")
    public String mainPage() {
        return "page/main";
    }

    @RequestMapping(value = "content", method = RequestMethod.GET)
    public String noticeBoard() {
        return "pc/content";
    }

    //@JmsListener(destination = "operateLogs")
    public void receiveoperateLogs(String message) {
        logger.info("处理消息：{}", message);
        OperateLog log = JSON.parseObject(message, OperateLog.class);
        operateLogMapper.insertSelective(log);
    }

    @RequestMapping(value = "errors")
    public String errorPage() {
        return "page/error";
    }
}
